package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.CollectionBean;
import com.jdew.thkj.bean.TbCollectionBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Hankkin on 16/1/30.
 */
public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<TbCollectionBean.DataBean> dataList;
    private LayoutInflater inflater;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String maType;
    private OnItemClickListener mItemClickListener;

    public MyAdapter(Context context, List<TbCollectionBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        maType = sp.getString("SpareTwo", "");
    }

    public void setDataList(List<TbCollectionBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoloder holoder = null;
        if (convertView == null) {
            holoder = new ViewHoloder();
            convertView = inflater.inflate(R.layout.product_list_item, null);
            holoder.product = (ImageView) convertView.findViewById(R.id.product);
            holoder.source = (ImageView) convertView.findViewById(R.id.source);
            holoder.price = (TextView) convertView.findViewById(R.id.price);
            holoder.before_price = (TextView) convertView.findViewById(R.id.before_price);
            holoder.num = (TextView) convertView.findViewById(R.id.num);
            holoder.quan = (TextView) convertView.findViewById(R.id.quan);
            holoder.name = (TextView) convertView.findViewById(R.id.name);
            holoder.name2 = (TextView) convertView.findViewById(R.id.name2);
            holoder.earn = (TextView) convertView.findViewById(R.id.earn);
            convertView.setTag(holoder);
        } else {
            holoder = (ViewHoloder) convertView.getTag();
        }

        TbCollectionBean.DataBean item = dataList.get(position);
        if (null != item && null != item.getUrl() && null != item.getClickUrl() && null != item.getVolume() && null != item.getIdentification() && null != item.getTitle()) {
            DecimalFormat df = new DecimalFormat("0.00");
            RequestOptions options = new RequestOptions().override(310, 310).placeholder(R.mipmap.ploder).diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(context).load(item.getUrl()).apply(options).into(holoder.product);
            holoder.before_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            if (null != item.getIdentification() && item.getIdentification().equals("0")) {
                holoder.source.setImageResource(R.mipmap.tb);
            } else {
                holoder.source.setImageResource(R.mipmap.tm);
            }
            holoder.before_price.setText("原价" + df.format(Double.parseDouble(item.getPrice())));
            holoder.price.setText(df.format(Double.parseDouble(item.getPrice()) - Double.parseDouble(item.getCoupon())) + "");
            holoder.num.setText(item.getVolume() == null ? "月销:0" : "月销:" + item.getVolume());
            holoder.quan.setText(null == item.getCoupon() ? "0元券" : item.getCoupon().replace(".00", "") + "元券");
            if (item.getTitle().length() > 16) {
                holoder.name.setText(item.getTitle().substring(0, 16));
                holoder.name2.setText(item.getTitle().substring(16, item.getTitle().length()));
            } else {
                holoder.name.setText(item.getTitle());
            }
            String zhuan = item.getCommission() == null ? "0.00" : item.getCommission();
            if (maType.equals("") || maType.equals("1")) {
                holoder.earn.setVisibility(View.GONE);
                SpannableString spannableString = new SpannableString(holoder.price.getText().toString());
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                spannableString.setSpan(sizeSpan, 0, holoder.price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                holoder.price.setText(spannableString);
            } else {
                holoder.earn.setVisibility(View.VISIBLE);
                holoder.earn.setText(zhuan);
            }
            SpannableString spannableString = new SpannableString(holoder.price.getText().toString());
            RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
            spannableString.setSpan(sizeSpan, 0, holoder.price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holoder.price.setText(spannableString);
        }
        return convertView;
    }

    class ViewHoloder {
        ImageView product;
        ImageView source;
        TextView price;
        TextView before_price;
        TextView num;
        TextView quan;
        TextView name;
        TextView name2;
        TextView earn;
    }
}
